<!DOCTYPE html>
<html lang="en">
<head>
    <link rel="stylesheet" href="../css/login.css">
    <link rel="stylesheet" href="../plugin/element-ui/index.css">
    <meta charset="UTF-8">
    <title>登录</title>
</head>
<body>
<div id="f_login_app">
    <div class="header">
        <div class="logo"><img src="../img/logo_new.png"/></div>
    </div>
    <div class="main">
        <el-form :model="ruleForm" :rules="rules" ref="ruleForm" >
            <el-form-item  prop="pass">
                <el-input placeholder="请输入账号" type="text" v-model="ruleForm.pass" autocomplete="off">
                    <i style="margin-top: 14px" slot="prefix" class="el-icon-user"></i>
                </el-input>
            </el-form-item>
            <el-form-item  prop="checkPass">
                <el-input placeholder="请输入密码" type="password" v-model="ruleForm.checkPass" autocomplete="off">
                    <i style="margin-top: 14px" slot="prefix" class="el-icon-lock"></i>
                </el-input>
            </el-form-item>
            <el-form-item  prop="age">
                <el-input placeholder="请输入验证码" v-model.number="ruleForm.age">
                    <i style="margin-top: 14px" slot="prefix" class="el-icon-key"></i>
                </el-input>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</div>
<script src="../plugin/vue/vue.js"></script>
<!-- 引入组件库 -->
<script src="../plugin/element-ui/index.js"></script>
<!-- 引入axios -->
<script src="../plugin/axios/axios.min.js"></script>
<script>
    let app = new Vue({
        el:"#f_login_app",
        data() {
            var checkAge = (rule, value, callback) => {
                if (!value) {
                    return callback(new Error('年龄不能为空'));
                }
                setTimeout(() => {
                    if (!Number.isInteger(value)) {
                        callback(new Error('请输入数字值'));
                    } else {
                        if (value < 18) {
                            callback(new Error('必须年满18岁'));
                        } else {
                            callback();
                        }
                    }
                }, 1000);
            };
            var validatePass = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请输入密码'));
                } else {
                    if (this.ruleForm.checkPass !== '') {
                        this.$refs.ruleForm.validateField('checkPass');
                    }
                    callback();
                }
            };
            var validatePass2 = (rule, value, callback) => {
                if (value === '') {
                    callback(new Error('请再次输入密码'));
                } else if (value !== this.ruleForm.pass) {
                    callback(new Error('两次输入密码不一致!'));
                } else {
                    callback();
                }
            };
            return {
                ruleForm: {
                    pass: '',
                    checkPass: '',
                    age: ''
                },
                rules: {
                    pass: [
                        { validator: validatePass, trigger: 'blur' }
                    ],
                    checkPass: [
                        { validator: validatePass2, trigger: 'blur' }
                    ],
                    age: [
                        { validator: checkAge, trigger: 'blur' }
                    ]
                }
            };
        },
        methods: {
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        alert('submit!');
                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });
            }
        }
    })
</script>
</body>
</html>